Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ApplicationRecord, an Active Record layer supertype #22567

Merged

Conversation

gsamokovarov
Copy link
Contributor

It's pretty common for folks to monkey patch ActiveRecord::Base to
work around an issue or introduce extra functionality. Instead of
shoving even more stuff in ActiveRecord::Base, ApplicationRecord can
hold all those custom work the apps may need.

Now, we don't wanna encourage all of the application models to inherit
from ActiveRecord::Base, but we can encourage all the models that do,
to inherit from ApplicationRecord.

Newly generated applications have app/models/application_record.rb
present by default. The model generators are smart enough to recognize
that newly generated models have to inherit from ApplicationRecord,
but only if it's present.

@rails-bot
Copy link

r? @chancancode

(@rails-bot has picked a reviewer for you, use r? to override)

@gsamokovarov
Copy link
Contributor Author

r? @rafaelfranca

@gsamokovarov gsamokovarov force-pushed the introduce-application-record branch 3 times, most recently from 2ed43e3 to 851deb6 Compare December 12, 2015 16:50
@claudiob
Copy link
Member

Do we need to test the case in which a user creates a model called ApplicationRecord?

It might not be too far-fetched as an option. Maybe 'ApplicationRecord' can be included in the words that cannot be used as a name for a model. Or a warning could be displayed if such a model exists.

@gsamokovarov
Copy link
Contributor Author

Do we need to test the case in which a user creates a model called ApplicationRecord?

That may be an idea. Maybe we can warn on some "reserved" names in all the NamedBase generators. For example if you do bundle exec rails generate controller Application, you end up with:

class ApplicationController < ApplicationController
end

@rafaelfranca
Copy link
Member

This is way too defensive to me. I don't think anyone would have a model
called ApplicationRecord

On Sat, Dec 12, 2015, 15:26 Genadi Samokovarov notifications@github.com
wrote:

Do we need to test the case in which a user creates a model called
ApplicationRecord?

That may be an idea. Maybe we can warn on some "reserved" names in all the
NamedBase generators. For example if you do bundle exec rails generate
controller Application, you end up with:

class ApplicationController < ApplicationControllerend


Reply to this email directly or view it on GitHub
#22567 (comment).

@claudiob
Copy link
Member

in that case, no objections 🎀

It's pretty common for folks to monkey patch `ActiveRecord::Base` to
work around an issue or introduce extra functionality. Instead of
shoving even more stuff in `ActiveRecord::Base`, `ApplicationRecord` can
hold all those custom work the apps may need.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rephrase the motivation. What I've understood to be the issue is that engines will force configuration changes onto an application, and that there's no way to keep them separate because both were configuring ActiveRecord::Base. That, in turn, results in monkey patches.

You are right about the convenience of where to include extra app wide record functionality.

cc @rafaelfranca

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! We can and should advertise that to plugin authors. I have AR extensions myself, which I can distribute as modules to be mixed into ApplicationRecord instead of ActiveRecord::Base monkey patches.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we need to rephrase the motivation to talk about engines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point wasn't whether or not we should advertise to engine authors. It was that the motivation stated here isn't why we want to add this class.

@claudiob
Copy link
Member

As part of this commit, you might also want to change these lines of the README so people learn not to inherit directly from ActiveRecord::Base anymore:

The library provides a base class that, when subclassed, sets up a mapping between the new class and an existing table in the database.

@claudiob
Copy link
Member

I am also wondering whether we should change tests/guides/docs to refrain from referencing ActiveRecord::Base whenever possible in favor of ApplicationRecord.

@gsamokovarov gsamokovarov force-pushed the introduce-application-record branch 2 times, most recently from c533f43 to 84da9c0 Compare December 15, 2015 17:56
@gsamokovarov
Copy link
Contributor Author

Yo! I tried to touch a bit on the documentation, I'm sure I haven't replaced all the references, but at least the Active Record guides are all covered. Still, the diff is reasonable and isn't blown up.

@kaspth can you check the changelog entry again?

ActiveRecord::Base.include(Yaffle::ActsAsYaffle)
# test/dummy/app/models/application_record.rb

class ApplicationRecord < ActiveRecord::Base
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to sell this, so I can hint the plugin folks. Tell me if you think it's an overkill.

@gsamokovarov gsamokovarov force-pushed the introduce-application-record branch 2 times, most recently from ce76f20 to 762e4df Compare December 16, 2015 09:23
It's pretty common for folks to monkey patch `ActiveRecord::Base` to
work around an issue or introduce extra functionality. Instead of
shoving even more stuff in `ActiveRecord::Base`, `ApplicationRecord` can
hold all those custom work the apps may need.

Now, we don't wanna encourage all of the application models to inherit
from `ActiveRecord::Base`, but we can encourage all the models that do,
to inherit from `ApplicationRecord`.

Newly generated applications have `app/models/application_record.rb`
present by default. The model generators are smart enough to recognize
that newly generated models have to inherit from `ApplicationRecord`,
but only if it's present.
@@ -1,3 +1,18 @@
* Introduce ApplicationRecord, an Active Record layer super type.

An `ApplicationRecord` let's engines have models, isolated from the main
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you folks pass over the motivation again?

rafaelfranca added a commit that referenced this pull request Dec 16, 2015
Introduce ApplicationRecord, an Active Record layer supertype
@rafaelfranca rafaelfranca merged commit 1d7d806 into rails:master Dec 16, 2015
@rafaelfranca
Copy link
Member

❤️ 💚 💙 💛 💜

@gsamokovarov
Copy link
Contributor Author

Thanks for the help, guys! I'll try to follow up with more documentation changes.

end

def determine_default_parent_class
if File.exist?('app/models/application_record.rb')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to expect that the app root is the current working directory here, or should it check for the file presence relative to application root?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check if thor has a helper for that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the check to be relative to the application root here 342221b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants